Exception Handling
An exception is a runtime error in a program that violates a system or application constraint, or a condition that is not expected to occur during normal operation. Examples are when a program tries to divide a number by zero or tries to write to a read-only file. When these occur, the system catches the error and raises an exception.
If the program has not provided code to handle the exception, the system will halt the
Program.
C# exception handling is managed via four keywords: try, catch, throw, and finally. Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception using catch and handle it in some rational manner. System-generated exceptions are automatically thrown by the C# runtime system. To manually throw an exception, use the keyword throw. Any code that absolutely must be executed upon exiting from a try block is put in a finally block.
Exception Classes
There are many different types of exceptions that can occur in a program. The BCL defines a number of exception classes, each representing a specific type of exception. When an exception occurs, the CLR
Methods of Exception Object
Program on exception handling
using System;
class Sample
{
public static void Main(String[] args)
{
int a = 10,b;
Console.WriteLine("Enter no");
b = Convert.ToInt32(Console.ReadLine());
try
{
a = a / b;
Console.WriteLine("Values of a" + a);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Divide by zero exception" + e.Message);
}
}
}
Program on Multiple catch blocks.
using System;
class Sample
{
public static void Main(String[] args)
{
int a = 10,b;
int[] c = { 10, 4, 2 };
Console.WriteLine("Enter no");
b = Convert.ToInt32(Console.ReadLine());
try
{
a = a / b;
c[20] = 5;
Console.WriteLine("Values of a" + a);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Divide by zero exception" + e.Message);
}
catch (IndexOutOfRangeException x)
{
Console.WriteLine("Array size problem");
}
}
}
Program on nested try statements.
using System;
class Sample
{
public static void Main(String[] args)
{
int a = 10,b;
int[] c = { 10, 4, 2 };
Console.WriteLine("Enter no");
b = Convert.ToInt32(Console.ReadLine());
try
{
a = a / b;
try
{
if (b == 1)
{
a = a / (b - 1);
}
else
{
c[20] = 5;
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Array size mismatch nested try error");
Console.WriteLine(e.StackTrace);
}
}
catch (DivideByZeroException e)
{
Console.WriteLine("Divide by zero exception" + e.Message);
}
}
}
Program on nested try statement with other method.
using System;
class Sample
{
public static void Main(String[] args)
{
int a = 10,b;
int[] c = { 10, 4, 2 };
Console.WriteLine("Enter no");
b = Convert.ToInt32(Console.ReadLine());
try
{
a = a / b;
Console.WriteLine("Value of a" + a);
put(b);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Divide by zero exception" + e.Message);
}
}
public static void put(int x)
{
int a=20;
int []c={2,33,22};
try
{
if (x == 1)
{
a = a / (x - 1);
}
else
{
c[20] = 5;
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Array size Miss match nested try error");
Console.WriteLine(e.StackTrace);
}
}
}
Program on Exception Class
using System;
class Sample
{
public static void Main(String[] args)
{
int a = 10, b;
int[] c = { 10, 4, 2 };
Console.WriteLine("Enter no");
b = Convert.ToInt32(Console.ReadLine());
try
{
a = a / b;
try
{
if (b == 1)
{
a = a / (b - 1);
}
else
{
c[20] = 5;
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Array size mismatch nested try error");
Console.WriteLine(e.StackTrace);
}
}
catch (Exception e)
{
Console.WriteLine( e.Message);
}
}
}
Program on throw.
using System;
class Sample
{
public static void Main(String[] args)
{
try
{
throw new DivideByZeroException("Vision thrown Divide by zero");
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
}
}
}
Program on Rethrow
using System;
class Rethrow
{
public static void genException()
{
// here, numer is longer than denom
int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 };
int[] denom = { 2, 0, 4, 4, 0, 8 };
for (int i = 0; i < numer.Length; i++)
{
try
{
Console.WriteLine(numer[i] + " / " +
denom[i] + " is " +
numer[i] / denom[i]);
}
catch (DivideByZeroException)
{
// catch the exception
Console.WriteLine("Can't divide by Zero!");
}
catch (IndexOutOfRangeException)
{
// catch the exception
Console.WriteLine("No matching element found.");
throw; // rethrow the exception
}
}
}
}
class vision
{
public static void Main()
{
try
{
Rethrow.genException();
}
catch (IndexOutOfRangeException)
{
// recatch exception
Console.WriteLine("Fatal error -- " +
"program terminated.");
}
}
}
Program on Finally
using System;
class Sample
{
public static void Main(String[] args)
{
int a = 10, b;
Console.WriteLine("Enter no");
b = Convert.ToInt32(Console.ReadLine());
try
{
a = a / b;
Console.WriteLine("Values of a" + a);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Divide by zero exception" + e.Message);
}
finally
{
Console.WriteLine("This is finally block");
}
}
}
Deriving exception class
Creating an exception type is easy. Just define a class derived from Exception. As a general rule, exceptions defined by you should be derived from ApplicationException since this is the hierarchy reserved for application-related exceptions.
Program on custom exception
using System;
class abc:ApplicationException
{
int a;
public abc(int x)
{
a=x;
}
public override string ToString()
{
return "My Exception[" + a + "]";
}
}
class Sample
{
public static void get(int x)
{
try
{
if(x>10)
throw new abc(x);
Console .WriteLine ("ValueType of x"+x);
}
catch (abc p)
{
Console.WriteLine(p);
Console.WriteLine(p.Message);
}
}
public static void Main(String[] args)
{
get(5);
get(25);
}
}